<?php
/**
 * The form for updating a customer.
 * @author Ian Dunn <scholar42@gmail.com>
 * @version 1.0
 * @since 2013-11-20
 */
require_once "login_info.php";
require_once "customer.php";
loginInfo::update_login();
?>


<html>
	<head>
		<title>Opol - Update Customer</title>
	</head>
	<body>
		<link rel="stylesheet" type="text/css" href="opol.css">
		<?php require 'menu.php' ?>
		<br>
<?php
if ($_POST) {
	$db = new database();

	if (isset($_POST['cus_id'])) {
		// Update the customer's information, and move on.
		$cus = new customer ((int)$_POST['cus_id']);
		$cus->init_from_post ($_POST);
		$result = $cus->update_database_by_id ($db);
		if (!$result) {
			echo "You do not have sufficient permissions to perform this action.<br>\n";
		} else {
			echo "Database Successfully Updated!<br>\n";
		}
		echo "<a href=\"/\">Back to main menu.</a><br>\n";
	} else {
		$cus_id = $_POST['name'];
		if (!$cus_id) {
			//echo "Please specify the customer's name.<br>\n";
		} else {

			$db = new database();

			$cus = new customer ($cus_id);
			$ret = $cus->init($db);
			if (!$ret) {
				echo "Error<br>\n";
			}
		}
	}

	$name 		= $cus->name;
	$phone_num 	= $cus->phone_num;
	$email 		= $cus->email;
	$address 	= $cus->address;

	$db->close ();

} else {
	header("location: update_entry.php?type=customer");
}

?>
		<form name="customer" action="" method="post">
			<input type="hidden" name="cus_id" value="<?php print $cus_id;?>">
		<table>
			<tr>
				<td><label>Name:</label></td>
				<td><input type="text" name="name" value="<?php print $name;?>"></td>
			</tr>

			<tr>
				<td><label>Email Address:</label></td>
				<td><input type="text" name="email" value="<?php print $email;?>"></td>
			</tr>

			<tr>
				<td><label>Phone Number:</label></td>
				<td><input type="text" name="phone_num" value="<?php print $phone_num;?>"></td>
			</tr>

			<tr>
				<td><label>Address:</label></td>
				<td><input type="text" name="address" value="<?php print $address;?>"></td>
			</tr>

			<tr>
				<td><input type="submit" value="Update"></td>
			</tr>
		</table>
		</form>
	</body>
</html>
